// (c) 2015 Teresa Holfeld
package com.holfeld.teresa.vocabtrainer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;


/**
 * An activity representing a list of Stacks. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link TrainVocabularyActivity} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 * <p/>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link VocabularyListFragment} and the item details
 * (if present) is a {@link TrainVocabularyFragment}.
 * <p/>
 * This activity also implements the required
 * {@link VocabularyListFragment.Callbacks} interface
 * to listen for item selections.
 *
 * @author Teresa Holfeld, teresa.holfeld@gmail.com
 */
public class VocabularyListActivity extends ActionBarActivity
        implements VocabularyListFragment.Callbacks {

    private static final int ADD_VOCABULARY_REQUEST = 0;
    private static final int SHOW_VOCABULARY_REQUEST = 1;

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vocabulary_list);

        if (findViewById(R.id.vocabulary_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((VocabularyListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.vocabulary_list))
                    .setActivateOnItemClick(true);
        }
    }

    /**
     * Callback method from {@link VocabularyListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String vocabularyName) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(TrainVocabularyFragment.ARG_VOCAB_NAME, vocabularyName);
            TrainVocabularyFragment fragment = new TrainVocabularyFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.vocabulary_container, fragment)
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, TrainVocabularyActivity.class);
            detailIntent.putExtra(TrainVocabularyFragment.ARG_VOCAB_NAME, vocabularyName);
            startActivity(detailIntent);
        }
    }

    @Override
    public void onAddVocabulary() {
        Intent addVocabularyIntent = new Intent(this, AddVocabularyActivity.class);
        startActivityForResult(addVocabularyIntent, ADD_VOCABULARY_REQUEST);
    }

    public void goToVocabulary(String vocabularyName) {
        Bundle arguments = new Bundle();
        arguments.putString(TrainVocabularyFragment.ARG_VOCAB_NAME,
                getIntent().getStringExtra(TrainVocabularyFragment.ARG_VOCAB_NAME));
        Intent stackIntent = new Intent(this, VocableListActivity.class);
        stackIntent.putExtra(TrainVocabularyFragment.ARG_VOCAB_NAME, vocabularyName);
        startActivityForResult(stackIntent, SHOW_VOCABULARY_REQUEST);
    }
}
